'''

Case Type： 数据库系统
Case Name： 执行事务时修改stat文件名字是否会影响
Create At:  2020/06/30
Owner    :  @li-xin12345
Descption:  1.启动数据库 2.集群循环运行业务 3.重命名gaussdb.state文件

history：
created： 2020/07/01  created by @li-xin12345
modified：2020/09/03  modified by @wan005 modify the name of bak file
'''
import os
import sys
import unittest
from yat.test import Node
from yat.test import macro
import time

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import *


class RebuildState(unittest.TestCase):
    com = CommonSH()
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info('------------Opengauss_Reliability_Dbsys_Case095.py start------------')
        self.SOURCE_PATH = macro.DB_ENV_PATH
        self.DN1_PATH = macro.DB_INSTANCE_PATH
        self.state_file_path = os.path.join(macro.DB_INSTANCE_PATH, macro.STATE_FILE_NAME)

        self.dbUserNode = Node(node='dbuser')

        self.Constant = Constant()
        self.log.info('----------------------------检查数据库状态-----------------------------')
        db_status = self.com.getDbClusterStatus('status')
        if not db_status:
            raise Exception("db status is not true, please check!")
        self.log.info(db_status)

    def test_RebuildState(self):
        # 循环运行业务
        self.log.info('----------------------------循环运行业务-----------------------------')
        sql = '''create table if not exists t_test(t_id integer, t_desc character(100));'''
        msg = self.com.executDbSql(sql)
        self.log.info(msg)
        for error_msg in self.Constant.SQL_WRONG_MSG:
            self.assertNotIn(error_msg, msg)
        sql = f"insert into t_test values (generate_series(1,100000),'aaa');"
        msg = self.com.executDbSql(sql)
        self.log.info(msg)
        for error_msg in self.Constant.SQL_WRONG_MSG:
            self.assertNotIn(error_msg, msg)
        for i in range(10):
            sql = f'select * from t_test limit {i + 1};'
            msg = self.com.executDbSql(sql)
            self.log.info(msg)
            for error_msg in self.Constant.SQL_WRONG_MSG:
                self.assertNotIn(error_msg, msg)
            if i == 5:
                self.log.info('----------------------------更改文件名字-----------------------------')
                mv_cmd = f'''mv {self.state_file_path} {self.state_file_path}.baktest'''
                self.log.info(mv_cmd)
                mv_msg = self.dbUserNode.sh(mv_cmd).result()
                self.assertEqual(mv_msg, '')
        sql = f"drop table t_test;"
        msg = self.com.executDbSql(sql)
        self.log.info(msg)
        for error_msg in self.Constant.SQL_WRONG_MSG:
            self.assertNotIn(error_msg, msg)
        self.log.info('---------------------------循环运行业务执行完毕------------------------------')

        self.log.info('---------------------------重启数据库------------------------------')
        self.com.stopDbCluster()
        self.com.startDbCluster()
        time.sleep(40)

        self.log.info('----------------------------检查数据库状态-----------------------------')
        db_status = self.com.getDbClusterStatus('status')
        if not db_status:
            raise Exception("db status is not true, please check!")
        self.log.info(db_status)

        self.log.info('---------------------------查看文件是否重建成功------------------------------')
        ls_cmd = f'ls {self.state_file_path}'
        self.log.info(ls_cmd)
        ls_msg = self.dbUserNode.sh(ls_cmd).result()
        self.log.info(ls_msg)
        self.assertNotIn(self.Constant.NO_FILE_MSG, ls_msg)

    def tearDown(self):
        self.log.info('---------------------------还原：删除备份文件------------------------------')
        rm_cmd = f"rm -rf {self.state_file_path}.baktest "
        self.log.info(rm_cmd)
        rm_msg = self.dbUserNode.sh(rm_cmd).result()
        self.assertEqual(rm_msg, '')
        self.log.info('Successful rebuild gaussdb.state')
        self.log.info('--------------Opengauss_Reliability_Dbsys_Case095 执行完成------------------------')
